jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("z-index","9999");
    this.css("top", Math.max(0, (($(window).height() - this.outerHeight()) / 2) + 
                                                $(window).scrollTop()) + "px");
    this.css("left", Math.max(0, (($(window).width() - this.outerWidth()) / 2) + 
                                                $(window).scrollLeft()) + "px");
    return this;
}

function reacomodarLinks(){
    var cuarter = $(window).width()/4;
    $(".page-registro .logo").css('left',cuarter -135);
    $(".page-registro .login").css('left',cuarter*3 - 174);

    var arriba = ($(window).height()-$("#registrar-modal").height())/4;

    if(arriba < 20)
        arriba = 20;
    if(arriba > 80)
        arriba = 80;
    $(".page-registro .logo").css('top',arriba -20);
    $(".page-registro .login").css('top',arriba -20);
}
$(document).ready(function() {
    $("#registrar-modal").center();
    // When everything has loaded, place all polaroids on a random position	
    var zindexnr = 1;
    $(".polaroid").each(function (i) {
            var zindex = 1;
            var tempVal = Math.round(Math.random());
            if(tempVal == 1) {
                    var rotDegrees = randomXToY(330, 360); // rotate left
            } else {
                    var rotDegrees = randomXToY(0, 30); // rotate right
            }

            // Internet Explorer doesn't have the "window.innerWidth" and "window.innerHeight" properties
            /*if(window.innerWidth == undefined) { 
                    var wiw = 1000;
                    var wih = 700;
            } else {
                    var wiw = window.innerWidth;
                    var wih = window.innerHeight;	
            }*/
			var wiw = $(window).width();
			var wih = $(window).height();
            if(i<3){
                var left = Math.random()*(100);
                var top = wih/3*i +50;
                zindex = 3;
            } else if(i>=3 && i<6){
                var left = Math.random()*(80) + wiw - 360;
                var top = wih/4*(i-3)+50;
                zindex = 3;
            } else{
                var left = Math.random()*(wiw-700)+200;
                var top = Math.random()*(wih-300)
            }
            
            var cssObj = {'left' : left,
                    'top' : top,
                    'z-index':zindex,
                    '-webkit-transform' : 'rotate('+ rotDegrees +'deg)',  // safari only
                    '-ms-transform' : 'rotate('+ rotDegrees +'deg)',  // ie9 only
                    '-moz-transform' : 'rotate('+ rotDegrees +'deg)',  // firefox only
                    '-o-transform' : 'rotate('+ rotDegrees +'deg)',  // opera only
                    'tranform' : 'rotate('+ rotDegrees +'deg)'}; // added in case CSS3 is standard
            $(this).css(cssObj);
    });

    // Set the Z-Index (used to display images on top while dragging)
    

    // boolean to check if the user is dragging
    var dragging = false;

    // Show the polaroid on top when clicked on
    $(".polaroid").mouseup(function(e){
            if(!dragging) {
                    // Bring polaroid to the foreground
                    zindexnr++;
                    var cssObj = {'z-index' : zindexnr,
                    'transform' : 'rotate(0deg)',	 // added in case CSS3 is standard
                    '-ms-transform' : 'rotate(0deg)',  // ie9 only
                    '-moz-transform' : 'rotate(0deg)',  // firefox only
                    '-o-transform' : 'rotate(0deg)',  // opera only
                    '-webkit-transform' : 'rotate(0deg)'};  // safari only
                    $(this).css(cssObj);
            }
    });

    // Make the polaroid draggable & display a shadow when dragging
    $(".polaroid").draggable({
            cursor: 'crosshair',
            start: function(event, ui) {
                    dragging = true;
                    zindexnr++;
                    var cssObj = {'box-shadow' : '#888 5px 10px 10px', // added in case CSS3 is standard
                            '-webkit-box-shadow' : '#888 5px 10px 10px', // safari only
                            'margin-left' : '-10px',
                            'margin-top' : '-10px',
                            'z-index' : zindexnr};
                    $(this).css(cssObj);
            },
            stop: function(event, ui) {
                    var tempVal = Math.round(Math.random());
                    if(tempVal == 1) {
                            var rotDegrees = randomXToY(330, 360); // rotate left
                    } else {
                            var rotDegrees = randomXToY(0, 30); // rotate right
                    }
                    var cssObj = {'box-shadow' : '', // added in case CSS3 is standard
                            '-webkit-box-shadow' : '', // safari only
                            'transform' : 'rotate('+ rotDegrees +'deg)', // added in case CSS3 is standard
                            '-ms-transform' : 'rotate('+ rotDegrees +'deg)',  // ie9 only
                            '-moz-transform' : 'rotate('+ rotDegrees +'deg)',  // firefox only
                            '-o-transform' : 'rotate('+ rotDegrees +'deg)',  // opera only
                            '-webkit-transform' : 'rotate('+ rotDegrees +'deg)', // safari only
                            'margin-left' : '0px',
                            'margin-top' : '0px'};
                    $(this).css(cssObj);
                    dragging = false;
            }
    });

    // Function to get random number upto m
    // http://roshanbh.com.np/2008/09/get-random-number-range-two-numbers-javascript.html
    function randomXToY(minVal,maxVal,floatVal) {
            var randVal = minVal+(Math.random()*(maxVal-minVal));
            return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
    }
    
    textos = fisher_yates(textos);
    mostrarGlobo();

});
var txtlzr;
function fisher_yates(array){ // v2005-06-01
    var i=array.length;
    while(i--){
        var j=Math.floor( Math.random() * (i+1) );
        var tmp=array[i];
        array[i]=array[j];
        array[j]=tmp;
    }
    return array;
}

function mostrarGlobo(){
    $(".txt-imagen").hide('slow');
    setTimeout(function(){
        $('.txt-imagen').remove();
        var j = Math.floor(Math.random()*6);
        $(".polaroid").each(function (i) {
            var randomnumber=Math.floor(Math.random()*999999)
            if(j == i){
                var clase = "txt-imagen-right txt-imagen-right";
                var iz = $(this).css('left').replace("px","");
                iz = iz - 200;
                if(i<3){
                    clase = "txt-imagen-left txt-imagen-left";
                    iz = iz+150;
                }
                var randomclase=Math.floor(Math.random()*10);
                clase += "-color-"+randomclase;
                $("body").append("<div id='"+randomnumber+"' class='txt-imagen "+clase+"' style='display:none;position:absolute; top:"+$(this).css('top')+"; left:"+iz+"px;'></div>");
                $("#"+randomnumber).show('slow');
                
                setTimeout(function(){
                    var list = [textos[i].txt1, '¡Regístrate ahora!',''];  // la lista de textos a mostrar
                    txtlzr = $('#'+randomnumber);  // el contenedor
                    var options = {
                        duration: 5000, // el tiempo que el texto permanecerá visible
                        rearrangeDuration: 300, // ella duración del efecto
                        effect: 'random', // el efecto que puede ser fadeIn, slideLeft, slideTop o random
                        centered: true // si queremos que se centre
                    }
                    txtlzr.textualizer(list, options); // enviamos los datos
                    txtlzr.textualizer('start'); // y lo ejecutamos
                }, 900);

                
            }
        });
    }, 900);
    
}